$(function () {

    //等待加载
    loading();
    var adv_id = getQueryString("adv_id");
    ajaxmyad(adv_id, null, 1);
    //ajaxmyadpic(adv_id,null,1);
    //oversize("productName", 18);
})

/**
 * 图片广告和视频广告切换
 */
$(function () {
    var adv_id = getQueryString("adv_id")
    $(".vedioAD").click(function () {
        $(".tcdPageCode").empty().unbind();
        $("#void").empty();
        //等待加载
        loading();
        $("#void").show();
        $("#pic").hide();
        $(".picAD").removeClass("current");
        $(this).addClass("current");
        ajaxmyad(adv_id, null, 1);
    });
    $(".picAD").click(function () {
        $(".tcdPageCode").empty().unbind();
        $("#pic").empty();
        //等待加载
        loading();
        $("#void").hide();
        $("#pic").show();
        $(".vedioAD").removeClass("current");
        $(this).addClass("current");
        var ad_type = "picture";
        ajaxmyadpic(adv_id, ad_type, 1);

    })
})
/**
 * 查询我的订阅的广告主下的所有广告
 * @param adv_id 广告主Id
 * @param ad_type 广告类型 video是视频广告，picture是图片广告
 * @param page 当前页数
 * @returns
 */
function ajaxmyad(adv_id, ad_type, page) {
    var url = "/Lvscore_User_M/myAds/ajaxmyad";
    $.post(url, {
        adv_id: adv_id,
        ad_type: ad_type,
        page: page
    }, function (data) {
        // sumpage：页容量（页面的数据条数）
        // nowpage：页码
        // adsList：List<AdInfoBean>
        // id:广告Id
        // name:广告名称
        // ad_type:广告类型
        // pc_size:广告大小
        // pc_version_url:广告路径
        // price:广告价格
        // 	:投放结束时间
        // play_num:播放次数
        $("#void").empty();
        if (data.adsList.length == 0) {
            $("#void").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">暂时没有视频广告</p>')
        }
        $.each(data.adsList, function (index, element) {
            if (!isNotEmpty(element.ad_homepage)) {
                element.ad_homepage = "/Lvscore_User_M/userWeb/static/images/bm.png";
            }
            if (!isNotEmpty(element.play_num)) {
                element.play_num = '未知';
            }
            if (!isNotEmpty(element.pc_size)) {
                element.pc_size ='未知';
            }
            $("#void").append("<li>" +
                "<span class='hintbg'></span>" +
                "<span class='center'><span class='productName'>" + getTitle(element.name) + "</span><b><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png' alt='' title=''>" + element.price + "</b></span>" +
                "<img src='" + element.ad_homepage + "' class='bjHover' width='235px' height='135px'>" +
                "<div class='backdrop'>" +
                "<div class='bj'></div>" +
                "<h2>" + getTitle(element.name) + "</h2>" +
                "<p>有效期至：" + element.plan_end + "</p>" +
                "<i>" + element.duration + "&quot;</i>" +
                "<ol>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png'></span>" + element.price + "</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon2.png'></span>" + element.pc_size + "</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon3.png'></span>" + element.play_num + "次</li>" +
                "</ol>" +
                "<input type='button' ad_type='video' ad_id='" + element.id + "' value='播放' class='plays'>" +
                "</div>" +
                "</li>");
            $(".recommendBox ul li").hover(function () {
                $(this).find(".backdrop").show();
            }, function () {
                $(this).find(".backdrop").hide()
            });

        });
        playAd();
        if ($(".tcdPageCode").html() == "") {
            createPageVideo(data.sumpage, data.nowpage);
        }
        //等待加载的效果消失
        $(".loadingBox").hide();

    }, "json");
}

/**
 * 图片广告
 * @param adv_id广告主ID
 * @param ad_type广告类型
 * @param page当前页数
 * @returns
 */
function ajaxmyadpic(adv_id, ad_type, page) {
    var url = "/Lvscore_User_M/myAds/ajaxmyad";
    $.post(url, {
        adv_id: adv_id,
        ad_type: ad_type,
        page: page
    }, function (data) {

        $("#pic").empty();
        if (data.adsList.length == 0) {
            $("#pic").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">暂时没有图片广告</p>')
        }
        $.each(data.adsList, function (index, element) {
            if (!isNotEmpty(element.ad_homepage)) {
                element.ad_homepage = "/Lvscore_User_M/userWeb/static/images/bm.png";
            }
            if (!isNotEmpty(element.duration)) {
                element.duration = '未知';
            }
            if (!isNotEmpty(element.play_num)) {
                element.play_num = '未知';
            }
            if (!isNotEmpty(element.pc_size)) {
                element.pc_size ='未知';
            }
            $("#pic").append("<li>" +
                "<span class='hintbg'></span>" +
                "<span class='center'><span class='productName'>" + getTitle(element.name) + "</span><b><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png' alt='' title=''>" + element.price + "</b></span>" +
                "<img src='" + element.ad_homepage + "' class='bjHover'>" +
                "<div class='backdrop'>" +
                "<div class='bj'></div>" +
                "<h2>" + getTitle(element.name) + "</h2>" +
                "<p>有效期至：" + element.plan_end + "</p>" +
                "<i>" + element.duration + "张</i>" +
                "<ol>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png'></span>" + element.price + "</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon2.png'></span>" + element.pc_size +"</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon3.png'></span>" + element.play_num + "次</li>" +
                "</ol>" +
                "<input type='button' ad_type='pic' ad_id='" + element.id + "' value='播放' class='plays'>" +
                "</div>" +
                "</li>");
            $(".recommendBox ul li").hover(function () {
                $(this).find(".backdrop").show();
            }, function () {
                $(this).find(".backdrop").hide()
            });
        });
        playAd();

        if ($(".tcdPageCode").html() == "") {
            createPagePic(data.sumpage, data.nowpage);
        }
        //等待加载的效果消失
        $(".loadingBox").hide();

    }, "json");
}

/**
 * 图片广告分页
 * @param pages当前页数
 * @param pageNum总页数
 * @returns
 */
function createPagePic(pages, pageNum) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                var adv_id = getQueryString("adv_id");
                ajaxmyadpic(adv_id, "picture", p);
            }
        });
    }
}

/**
 * 视频广告分页
 * @param pages当前页数
 * @param pageNum总页数
 * @returns
 */
function createPageVideo(pages, pageNum) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                var adv_id = getQueryString("adv_id");
                ajaxmyad(adv_id, "video", p);
            }
        });
    }
}	
	